﻿//----------------------------------------------------------------
// Copyright (c) 2011-2013 
// All rights reserved.
// Author: Ben DaoWei 
// Contact: bendaowei@gmail.com
// Web Link: http://xnaeditor.codeplex.com
// ----------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine.Core;
using Microsoft.Xna.Framework;
using System.IO;
using UnityEngine.IO;
using Microsoft.Xna.Framework.Graphics;
using UnityEngine.Graphics;
using UnityEngine.Graphics.Texture;

namespace UnityEngine.Terrain
{
    public class Landscape : UnityEngine.Core.Component
    {
        private TexturePalette[] _TexturePallettes;

        public TexturePalette[] TexturePallettes
        {
            set { _TexturePallettes = value; }
            get { return _TexturePallettes; }
        }

        public LandscapeConfig LandscapeConfig
        {
            get;
            set;
        }

        public WarCraftEnvironment WarCraftEnvironment
        {
            get;
            set;
        }

        public WarCraftMapDoo WarCraftMapDoo
        {
            get;
            set;
        }

        public WarCraftMapPath WarCraftMapPath
        {
            get;
            set;
        }

        public Landscape()
            : this(null)
        {
        }

        public Landscape(GameObject gameObject)
            : base(gameObject)
        {
        }

        public override void Startup(Microsoft.Xna.Framework.GameTime gameTime)
        {
            if (this.GameObject.HasComponent("LandscapeConfig") == false)
                this.GameObject.AddComponent("LandscapeConfig");
            this.LandscapeConfig = this.GameObject.GetComponent("LandscapeConfig") as LandscapeConfig;

            string path;

            // load landscape path first, information of heights are inserted during the generation of world environment
            path = IOManager.Instance.GetAssertPath(this.LandscapeConfig.WarCraftPathFileName);
            using (FileStream fileStream = new FileStream(path, FileMode.Open))
            {
                WCLoader loader = new WCLoader(path, fileStream);
                this.WarCraftMapPath = new WarCraftMapPath(this, loader);
                //this.WarCraftMapPath.SaveToBmp("MapPath.bmp");
            }

            // load environment textures
            this.TexturePallettes = new TexturePalette[this.LandscapeConfig.TexturePaletteFileNames.Length];
            for (int i = 0; i < this.LandscapeConfig.TexturePaletteFileNames.Length; i++)
            {
                this.TexturePallettes[i] = new TexturePalette();
                this.TexturePallettes[i].Texture = TextureManager.Instance.GetTexture(this.LandscapeConfig.TexturePaletteFileNames[i]);
            }

            // load environment 
            path = IOManager.Instance.GetAssertPath(this.LandscapeConfig.WarCraftEnvironmentFileName);
            using (FileStream fileStream = new FileStream(path, FileMode.Open))
            {
                WCLoader loader = new WCLoader(path, fileStream);
                this.WarCraftEnvironment = new WarCraftEnvironment(this, loader);
                this.WarCraftEnvironment.GenerateGraphics();
            }

            // load map doodads
            path = IOManager.Instance.GetAssertPath(this.LandscapeConfig.WarCraftMapDooFileName);
            using (FileStream fileStream = new FileStream(path, FileMode.Open))
            {
                WCLoader loader = new WCLoader(path, fileStream);
                this.WarCraftMapDoo = new WarCraftMapDoo(this, loader);
                this.WarCraftMapDoo.GenerateGraphics();
            }

            base.Startup(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            if (this.WarCraftEnvironment != null)
                WarCraftEnvironment.Draw(gameTime);
            base.Draw(gameTime);
        }

        public override int Priority
        {
            get
            {
                return 100;
            }
        }
    }
}
